<?php
class DefaultStyle extends ObjectModel 
{
    public $id;
    public $id_cms;
    public $selector;
    public $style;
    public $active;
    
    public static $definition = array(
        'table' => 'pt_default_style',
        'primary' => 'id_style',
        'multilang' => false,
        'fields' => array(
            'id_cms' => array(
                'type' => ObjectModel::TYPE_INT,
                'required' => true, 
                'validate' => 'isUnsignedId'
            ),
            'selector' => array(
                'type' => ObjectModel::TYPE_STRING,
                'required' => true, 
            ),
            'style' => array(
                'type' => ObjectModel::TYPE_STRING,
            ),
            'active' => array(
                'type' => ObjectModel::TYPE_BOOL,
                'required' => true
            )
        )
    );
    
    public function getCMSName ()
    {
        $cms = new PrestathemeCms ($this->id_cms, Context::getContext()->language->id);
        if ($cms)
            return $cms->getCMSName();
        else
            return NULL;
    }
    
    public static function getDefaultStyles ($active = true, $filter = '')
    {
        $sql = '
            SELECT ds.id_style, CONCAT(ds.selector, \' (\',c.`code`, \' \', c.`version`, \')\') AS selector
            FROM `' . _DB_PREFIX_ . 'pt_default_style` ds
            LEFT JOIN `' . _DB_PREFIX_ . 'pt_cms` c ON (ds.`id_cms` = c.`id_cms`)
            WHERE 1'. ($active ? ' AND ds.`active` = 1' : '') . $filter;
        if ($rows = Db :: getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql)) 
        {
            return $rows;
        }
        return array();
    }
}